﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Configuration;
namespace LybrixNursingCRMDesktop.FrameWork
{
    class ConnectionFactory
    {
        public static EntityConnection getLibraryConnection()
        {
            // Specify the provider name, server and database.
            string providerName = "System.Data.SqlClient";
            string serverName = ConfigurationManager.AppSettings["SERVER_NAME"];
            string databaseName = ConfigurationManager.AppSettings["DATABASE_NAME"];
            string userID = ConfigurationManager.AppSettings["USER"];
            string password = ConfigurationManager.AppSettings["PASSWORD"];

            // Initialize the connection string builder for the
            // underlying provider.
            var sqlBuilder =
                new SqlConnectionStringBuilder();

            // Set the properties for the data source.
            sqlBuilder.DataSource = serverName;
            sqlBuilder.InitialCatalog = databaseName;
            sqlBuilder.IntegratedSecurity = false;
            sqlBuilder.UserID = userID;
            sqlBuilder.Password = password;
            sqlBuilder.MultipleActiveResultSets = true;

            // Build the SqlConnection connection string.
            string providerString = sqlBuilder.ToString();

            // Initialize the EntityConnectionStringBuilder.
            var entityBuilder =
                new EntityConnectionStringBuilder();

            //Set the provider name.
            entityBuilder.Provider = providerName;

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = providerString;

            // Set the Metadata location.
            entityBuilder.Metadata =
                @"res://*/NursingDB.csdl|
              res://*/NursingDB.ssdl|
              res://*/NursingDB.msl";

            return new EntityConnection(entityBuilder.ToString());
        }
    }
}

